Page({
  data:{
    passport:{}
  },
  onLoad({id, encryptedData}){
    console.log(id,encryptedData)
    // 获取访客详情
    this.getPassport(id)
    this.getPassportShare(encryptedData)
  },
  async getPassportShare(encryptedData){
    if(!encryptedData) return
    // 调用接口
    const {code ,data:passport} = await wx.http.get('/visitor/share/'+encryptedData)
    // 检测接口是否调用成功
    if(code !== 10000) return wx.utils.toast()
    // 渲染数据
    this.setData({passport})
  },
  // 获取访客详情（通行证）
  async getPassport(id){
    if(!id) return
    // 调用接口
    const {code ,data:passport} = await wx.http.get('/visitor/'+id)
    // 检测接口是否调用成功
    if(code !== 10000) return wx.utils.toast('获取通行证失败')
    // 渲染数据
    this.setData({passport})
  },
  onShareAppMessage() {
    const {encryptedData} = this.data.passport
    return {
      title: '查看通行证',
      path: '/visitor_pkg/pages/passport/index?encryptedData='+encryptedData,
      imageUrl: 'https://enjoy-plus.oss-cn-beijing.aliyuncs.com/images/share_poster.png',
    }
  },
  async saveQRCode(){
    try{
      // 读取图片信息
      const {path} = await wx.getImageInfo({
        src: this.data.passport.url
      })
      // console.log(res)
      // 保持图片到手机
      wx.saveImageToPhotosAlbum({
        filePath: path,
      })
    }catch(err){
      wx.utils.toast('保存图片失败，请稍后重试')
    }
    
  }
})
